//
//  ModifyAddressVC.m
//  wanzhaoIM
//
//  Created by liuyuantao on 15/5/8.
//  Copyright (c) 2015年 liuyuantao. All rights reserved.
//

#import "ModifyAddressVC.h"
#import "MicroDef.h"
#import "JKAlertDialog.h"
#import "CXmppRequestMgr.h"
#import "CXmppMgr.h"
#import "MBProgressHUD+WZ.h"
#import "UIUtil.h"
#import "UIBarButtonItem+category.h"

@interface ModifyAddressVC ()<UITextViewDelegate>
{
    UITextView *_messageTextView;
}
@end

@implementation ModifyAddressVC

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.navigationController setNavigationBarHidden:NO];
    [self.navigationItem setHidesBackButton:YES];
    [self.navigationItem setNewTitleCenter:@"修改地址"];
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:@"call_back" title:@"返回" target:self sel:@selector(back) titleFont:Font18 titleColor:WhiteColor];
    
    
     self.navigationItem.rightBarButtonItem = [UIBarButtonItem BarButtonItemWithImageName:nil title:@"保存" target:self sel:@selector(save) titleFont:Font18 titleColor:WhiteColor];
       
    _messageTextView = [[UITextView alloc] init];
    
    [_messageTextView setAutocorrectionType:UITextAutocorrectionTypeNo];
    [_messageTextView setReturnKeyType:UIReturnKeyDone];
    [_messageTextView.layer setCornerRadius:6.0f];
    _messageTextView.text = self.curUser.address;
    [_messageTextView setSelectedRange:NSMakeRange(self.curUser.address.length,0)];
    //	[messageTextView setBackgroundColor:ClearColor];
//    [_messageTextView setText:kPlaceholderText];
    //	[messageTextView setTextColor:[UIColor lightGrayColor]];
//    [_messageTextView setTextColor:KPlacehoderTextColor];
    
    _messageTextView.delegate = self;
    [_messageTextView setFont:[UIFont systemFontOfSize:18.0f]];
    [_messageTextView setTextColor:RGBCOLOR(0x32, 0x32, 0x32)];
    _messageTextView.layer.cornerRadius = 5.0f/2;
    _messageTextView.layer.masksToBounds = YES;
    _messageTextView.layer.borderWidth = 0.5f;
    _messageTextView.layer.borderColor = RGBCOLOR(0xd4, 0xd4, 0xd4).CGColor;
    [self.view addSubview:_messageTextView];

    
    CGFloat padding  = 10.0f;
    CGFloat hight = 100.0f;
    if(HARDWARE_SCREEN_HEIGHT >= 736)
    {
        hight = 700.0f/3.0f;
    }
    
    _messageTextView.frame = CGRectMake(padding, padding, HARDWARE_SCREEN_WIDTH - 2 * padding, hight);
    
    self.view.backgroundColor = RGBCOLOR(0xf3, 0xf3, 0xf3);
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveSuccess:) name:K_XMPPMODIFYINFO object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveFailed) name:K_XMPPDIDFAILEDRECEIVEIQ object:nil];
    
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPDIDFAILEDRECEIVEIQ object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:K_XMPPMODIFYINFO object:nil];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [_messageTextView resignFirstResponder];
}


- (void)textViewDidChange:(UITextView *)textView
{
    if (textView.text.length > 50) {
        textView.text = [textView.text substringToIndex:50];
        [MBProgressHUD showError:@"不能超过50个字符"];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if ([text isEqualToString: @"\n"]) {
        // Hide the keyboard
        [textView resignFirstResponder];
        
        // Also return if its showing a placeholder
        return NO ;
    }
    if ([text isEqualToString:@""]) {
        return YES;
    }
    
    if (textView.text.length > 49) {
        return NO;
    }
    
        return YES ;

}

-(void)back
{
    
    //    switch (self.vc_type.integerValue) {
    //        case 0:
    //        {
    //            if ([self.curUser.EXtenSion isEqualToString:_inputTX.text]) {
    //                [self.navigationController popViewControllerAnimated:YES ];
    //            } else {
    //
    //            }
    //        }
    //            break;
    //        case 1:
    //        {
    //            if ([self.curUser.MobileTelephone isEqualToString:_inputTX.text]) {
    //                [self.navigationController popViewControllerAnimated:YES ];
    //            } else {
    //
    //            }
    //        }
    //            break;
    //        case 2:
    //        {
    //            if ([self.curUser.EXtenSion isEqualToString:_inputTX.text]) {
    //                [self.navigationController popViewControllerAnimated:YES ];
    //            } else {
    //                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"您修改的信息是否需要保存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    //                [alert show];
    //            }
    //
    //        }
    //            break;
    //
    //        default:
    //            break;
    //    }
    
//    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"您修改的信息是否需要保存" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
//    [alert show];
    
    [_messageTextView resignFirstResponder];
    NSString *addressString =[[NSString alloc]init];
    addressString = _messageTextView.text;
    if ([self.curUser.address isEqualToString:addressString]) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }

    __block ModifyAddressVC *block = self;
    JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:nil message:@"您修改的信息是否需要保存?"];
    [alert addButton:Button_OK withTitle:@"是" handler:^(JKAlertDialogItem *item) {
        
        [self save];
    }];
    
    [alert addButton:Button_CANCEL withTitle:@"否" handler:^(JKAlertDialogItem *item) {
        
        [block.navigationController popViewControllerAnimated:YES ];
    }];
    [alert show];

}

//- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//{
//    switch (buttonIndex) {
//        case 0:
//        {
//            [self.navigationController popViewControllerAnimated:YES ];
//        }
//            break;
//        case 1:
//        {
//            [self save];
//        }
//            break;
//        default:
//            break;
//    }
//}


#pragma mark 通知中心方法
- (void)saveSuccess:(NSNotification *)notice
{
    [UIUtil hideMBProgressViewForView:self.view];
    self.curUser.address = _messageTextView.text;
    
    [MBProgressHUD showSuccess:@"保存成功"];
  [self.navigationController popViewControllerAnimated:YES];
//    JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:@"" message:@"保存成功"];
//    [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
//        
//        if ([block.delegate respondsToSelector:@selector(modifyInfo)]) {
//            [block.delegate modifyInfo];
//            [block.navigationController popViewControllerAnimated:YES ];
//        }
//    }];
//    [alert show];
    
}

- (void)saveFailed
{
    [UIUtil hideMBProgressViewForView:self.view];
    [MBProgressHUD showError:@"保存失败，请您检查网络"];
    [self.navigationController popViewControllerAnimated:YES];
//     __block ModifyAddressVC *block = self;
//    JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:@"" message:@"保存失败"];
//    [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
//        [block.navigationController popViewControllerAnimated:YES ];
//    }];
//    [alert show];
    
    
}
-(void)save
{
//    bug 6049
    [_messageTextView resignFirstResponder];
    if (_messageTextView.text.length > 50) {
//        JKAlertDialog *alert = [[JKAlertDialog alloc]initWithTitle:@"" message:@"您输入的内容超过50字！"];
//        [alert addButton:Button_OK withTitle:@"确定" handler:^(JKAlertDialogItem *item) {
//        }];
//        [alert show];
        [MBProgressHUD showError:@"您输入的内容超过50字"];
        return;
    }
    if (_messageTextView.text.length > 0) {
       [[CXmppRequestMgr shareInstance] updateSingleUserInfoWithDic:@{@"address":_messageTextView.text}];
    } else {
        [[CXmppRequestMgr shareInstance] updateSingleUserInfoWithDic:@{@"address":@""}];
    }
   
    
    [UIUtil addMBProgressViewTitle:@"正在保存..." inView:self.view alpha:0.5 failureTitle:@"保存失败，请检查网络"];
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
